package com.googlecode.framework.core.pagination;

public class Order implements java.io.Serializable {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7189764757220037421L;

    private String name;
    
    private boolean ascending;
    
    public static Order asc(String name) {
        return new Order(name, true);
    }

    public static Order desc(String name) {
        return new Order(name, false);
    }
    
    private Order(String name, boolean ascending) {
        this.name = name;
        this.ascending = ascending;
    }

    public String name() {
        return name;
    }

    public boolean isAscending() {
        return ascending;
    }
    
    @Override
    public boolean equals(Object o) {
        if (o instanceof Order) {
            Order another = (Order) o;
            return name().equals(another.name()) && isAscending() && another.isAscending();
        } else {
            return false;
        }
    }
    
    @Override
    public int hashCode() {
        int hash = 7;
        hash = hash*31 + this.name().hashCode();
        hash = hash*31 + (isAscending() ? 1 : 2);
        return hash;
    }
    
    @Override
    public String toString() {
        return name() + " " + (isAscending() ? "asc" : "desc");
    }
}
